<template>
	<sw-setting-frame tips="开启之后，账号设定天数未修改密码强制修改密码">
		<template slot="form">
			<h4>是否定时强制修改密码</h4>
			<el-switch v-model="formData.isTimingForce" active-value="1" inactive-value="2" @change="handleSaveForceConfig" :disabled="!isHasSetAuth" />
		</template>
		<template slot="body" v-if="formData.isTimingForce === '1'">
			<span>强制修改周期</span>
			<sw-custom-input size="mini" input-type="number" v-model="formData.timingForceLen" :disabled="!isHasSetAuth" />
			<font>天</font>
			<el-button size="mini" type="primary" v-hasPermi="settingButton" @click="handleSaveForceConfig">保存</el-button>
		</template>
	</sw-setting-frame>
</template>

<script>
import lodash from 'lodash';
import { mapGetters } from 'vuex';
import authBtns from '@/mixins/authButtons';
import { updateTimingSetting } from '@/api/system/tbSysSite';
export default {
	name: 'timeForceModifyPass',
	data() {
		return {
			formData: {}
		};
	},
	computed: {
		...mapGetters(['siteInfo', 'authButton']),
		settingButton() {
			let btns = authBtns[this.$options.name] || [];
			let setting = lodash.find(btns, { field: 'setting' });
			return setting?.id || '';
		},
		isHasSetAuth() {
			return lodash.some(this.authButton, { id: this.settingButton });
		}
	},
	mounted() {
		let fields = ['id', 'isTimingForce', 'timingForceLen'];
		this.formData = lodash.cloneDeep(lodash.pick(this.siteInfo, fields));
	},
	methods: {
		async handleSaveForceConfig() {
			let params = { ...this.formData };
			let result = await updateTimingSetting(params).then((res) => res.data);
			if (result.code === '0') {
				this.$showCustomMessage('success', `保存设置成功`);
				this.$store.dispatch('getSiteBaseInfo');
			}
		}
	}
};
</script>
